package com.swipemaster.levels.levelinfo;

import java.io.Serializable;

import com.swipemaster.common.Const;
import com.swipemaster.levels.Level;
import com.swipemaster.levels.LevelFactory;
import com.swipemaster.levels.PuzzleLevel;

/** Collection containing all information about levels. */
public class LevelsInfo implements Serializable
{
	private static final long serialVersionUID = 2;
	private final LevelInfo[] levelsInfo = new LevelInfo[Const.LEVELS];

	/** Construct all level informations with initial values. */
	public LevelsInfo()
	{
		for (int i = 0; i < Const.LEVELS; i++)
		{
			levelsInfo[i] = new LevelInfo();
			final Level level = LevelFactory.getLevel(i);
			levelsInfo[i].isPuzzle = level instanceof PuzzleLevel;
			levelsInfo[i].index = i;
		}
		unlockFirstLevels();
		setFreeLevels();
	}

	public LevelInfo getInfo(int levelIndex)
	{
		return levelsInfo[levelIndex];
	}

	/** @return which level of this type is this level (number of levels of the same type before this level)
	 * @throws Exception when index of level is incorrect */
	public int getLevelTypePosition(int index)
	{
		final boolean levelType = levelsInfo[index].isPuzzle;
		int position = 0;
		for (int i = 0; (i < Const.LEVELS) && (i < index); i++)
		{
			if (levelsInfo[i].isPuzzle == levelType)
			{
				++position;
			}
		}
		return position;
	}

	private void setFreeLevels()
	{
		for (int i = 0; i < Const.FREE_LEVELS; i++)
		{
			levelsInfo[i].isFree = true;
		}
	}

	/** Unlocks first level, so that user can start to play. */
	private void unlockFirstLevels()
	{
		levelsInfo[0].isLocked = false;
	}
}
